/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import systole.domain.signals.SignalFrequency;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.SignalFrequencySyncBroker;
import systole.synchronization.remote.entities.SignalFrequencyRemote;

/**
 *
 * @author Juan Manuel
 */
public class SignalFrequencySyncBrokerDB extends BrokerDB implements SignalFrequencySyncBroker {

    public void saveSignalFrequencyRemote(SignalFrequencyRemote signalFrequencyRemote) throws ExceptionDAO {
        try {
            this.logger.logDebug("saving remote signal frequency ");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(signalFrequencyRemote);
            this.logger.logDebug("remote signal frequency saved");
        } catch (HibernateException e) {
            this.logger.logError("error on save signal frequency remote, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron guardar los cambios", e.fillInStackTrace());
        }
    }

    public List<SignalFrequencyRemote> getSignalFrequenciesToUpdate() throws ExceptionDAO {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<SignalFrequency> getSignalFrequenciesToUpload() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting signal frequency to upload");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            String hql = "select s from SignalFrequencyRemote sr right join sr.frequency s where sr=null";
            Query query = currentSession.createQuery(hql);
            @SuppressWarnings("unchecked")
            List<SignalFrequency> list = query.list();
            this.logger.logDebug("get successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get signal frequency to upload, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los períodos de muestreo", e.fillInStackTrace());
        }
    }

    public SignalFrequencyRemote getFrequencyRemoteBySignalFrequency(SignalFrequency frequency) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting frequency remote by local frequency");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    SignalFrequencyRemote.class).add(Restrictions.eq("frequency", frequency)).setMaxResults(1);
            SignalFrequencyRemote frequencyRemote = (SignalFrequencyRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return frequencyRemote;
        } catch (HibernateException e) {
            this.logger.logError("error on get remote frequency by frequency, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los períodos de muestreo", e.fillInStackTrace());
        }
    }

    public int getLastRemoteSignalFrequencyIdSynchronized() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting last remote signal frequency sync");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    SignalFrequencyRemote.class).addOrder(Order.desc("remoteId")).setMaxResults(1);
            SignalFrequencyRemote frequencyRemote = (SignalFrequencyRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (frequencyRemote != null ? frequencyRemote.getRemoteId() : -1);
        } catch (HibernateException e) {
            this.logger.logError("error on get last remote signal frequency synchronized, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los períodos de muestreo", e.fillInStackTrace());
        }
    }

    public SignalFrequency getSignalFrequencyByRemoteId(int remoteId) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting signal frequency by remote id");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    SignalFrequencyRemote.class).add(Restrictions.eq("remoteId", remoteId)).setMaxResults(1);
            SignalFrequencyRemote signalFrequencyRemote = (SignalFrequencyRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (signalFrequencyRemote != null ? signalFrequencyRemote.getFrequency() : null);
        } catch (HibernateException e) {
            this.logger.logError("error on get signal frequency by remote id, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener los períodos de muestreo", e.fillInStackTrace());
        }
    }
}
